﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class EditDepositTransactionModel
    {
        public DepositHeader Header { get; set; }
        public DepositHeaderDTO HeaderDTO { get; set; }
        public DepositTransaction Transaction { get; set; }
        public EditDepositTransactionModel()
        {
            Init();
        }
        void Init()
        {
            if (null == Header)
                Header = new DepositHeader();
            if (null == Transaction)
                Transaction = new DepositTransaction();
            var defaultItem = WebUtil.SelectListSelectDefault();
            TypeOfPaymentList = new BindingList<SelectListItem> { defaultItem };

        }

        public static EditDepositTransactionModel BuildModel(DepositTransactionScreenData data)
        {
            var model = new EditDepositTransactionModel();
            if (data.Header != null)
                model.Header = data.Header;
            if (data.Transaction != null)
            {
                model.Transaction = data.Transaction;
                model.UpdateMode = true;
            }
            else
            {
                model.TransactionType = "Deposit";
            }

            model.HeaderDTO = new DepositHeaderDTO { Header = data.Header, InvolvedPartyType = data.InvolvedType };


            if (data.PaymentTypes != null || data.PaymentTypes.Any())
            {
                foreach (var selectListItem in data.PaymentTypes)
                {
                    model.TypeOfPaymentList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.PaymentTypeName, Value = selectListItem.PaymentTypeId.ToString() });
                }
            }
            model.HeaderID = model.Header.DepositHeaderID;
            model.TransactionID = model.Transaction.DepositTransactionID;

            return model;
        }


        #region dropdownlist data
        

        //Type of Payment (.TYPEOFPAYMENT_NAME)
        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedTypeOfPayment
        {
            get { return Transaction.TypeofpaymentID; }
            set { Transaction.TypeofpaymentID = value; }


        }
        public IList<SelectListItem> TypeOfPaymentList { get; set; }



        #endregion


        #region adapter data
        public int? HeaderID
        {
            get
            {
                return Transaction.DepositHeaderID;
            }

            set
            {
                Transaction.DepositHeaderID = value;
            }
        }
        public int TransactionID
        {
            get
            {
                return Transaction.DepositTransactionID;
            }

            set
            {
                Transaction.DepositTransactionID = value;
            }
        }


        //[DataType(DataType.Date)]
        //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        //[Display(Name = "Maturity Date")]
        //public DateTime? HeaderMaturityDate
        //{
        //    get { return Header.MaturityDate; }
        //    set { Header.MaturityDate = value; }
        //}

        //TransactionType
        [Required(ErrorMessage = "โปรดระบุ")]
        public string TransactionType
        {
            get { return Transaction.TransactionType; }
            set { Transaction.TransactionType = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public string RefNo
        {
            get { return Transaction.RefNO; }
            set { Transaction.RefNO = value; }
        }

        //[Required(ErrorMessage = "โปรดระบุ")]
        //[DataType(DataType.Date)]
        //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        //[Display(Name = "Trade Date")]
        //public DateTime? TradeDate
        //{
        //    get { return Transaction.TradeDate; }
        //    set { Transaction.TradeDate = value; }
        //}
        [Required(ErrorMessage = "โปรดระบุ")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Settlement Date")]
        public DateTime? SettlementDate
        {
            get { return Transaction.SettlementDate; }
            set
            {
                Transaction.SettlementDate = value;
                Transaction.TradeDate = value;
            }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public int? Amount
        {
            get { return Transaction.Amount; }
            set { Transaction.Amount = value; }
        }


        #endregion


        public String SubmitBtn { get; set; }
        public String CancelBtn { get; set; }
        public bool UpdateMode { get; set; }
    }
}